//
//  KKBaseTabController.swift
//  MVVMSwfit
//
//  Created by CarShare on 2018/12/3.
//  Copyright © 2018年 CarShare. All rights reserved.
//

import UIKit

class KKBaseTabController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.configChildVC()
    }
}
//MARK: ---设置界面
extension KKBaseTabController {
    
    private func configChildVC(){
    
        let arr = [["clsName":"KKHomeVController",
                    "title":"首页",
                    "imageName":"home_gray",
                    "selectImage":"home"],
                   ["clsName":"KKOrderVController",
                    "title":"订单",
                    "imageName":"mine_gray",
                    "selectImage":"mine"],
                   ["clsName":"KKMineVController",
                    "title":"我的",
                    "imageName":"mine_gray",
                    "selectImage":"mine"]]
        var arrM = [UIViewController]()

        for dict in arr {
            arrM.append(controller(dict: dict))
        }
        viewControllers = arrM
    }
    
    /// 使用字典设置自控制器属性
    ///
    /// - Parameter dict: 信息字典 [clsName,title,imageName]
    /// - returns 自控制器
    private func controller(dict:[String:String]) -> UIViewController{
        
        let nameSpace = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let selectImage = dict["selectImage"],
            let vcClass = NSClassFromString(nameSpace + "." + clsName) as? UIViewController.Type else{
                return UIViewController()
        }
        let vc = vcClass.init()

        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named:
            selectImage)?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes(
            [NSAttributedString.Key.foregroundColor:UIColor.gray,
             NSAttributedString.Key.font:UIFont.systemFont(ofSize: 11)], for: .normal)
        vc.tabBarItem.setTitleTextAttributes(
            [NSAttributedString.Key.foregroundColor:UIColor.blue,
             NSAttributedString.Key.font:UIFont.systemFont(ofSize: 11)], for: .selected)
        
        let nav = KKBaseNaviController.init(rootViewController: vc)
        
        return nav;
    }
}
